﻿using Autofac;
using Autofac.Extras.DynamicProxy2;
using Castle.DynamicProxy;
using System;

namespace AopOfAutofacCastleDemo
{
    /// <summary>
    /// 
    /// </summary>
    internal class Program
    {
        private static IContainer container;

        private static void Main(string[] args)
        {
            var builder = new ContainerBuilder();
            // Named registration
            builder.Register(c => new CallLogger(Console.Out)).Named<IInterceptor>("log-calls");
            // Typed registration
            builder.Register(c => new CallLogger(Console.Out));
            // Typed registration
            builder.Register(c => new ExceptionHandler(Console.Out));
            // Enable Interception on Types
            builder.RegisterType<PersonRepository>()
               .EnableInterfaceInterceptors()
               .As<IPersonRepository>();
            // Using the TYPED attribute:
            builder.RegisterType<First>().EnableClassInterceptors();
            // Using the NAMED attribute:
            builder.RegisterType<Second>().EnableClassInterceptors();
            // Using the InterceptedBy Method:
            builder.RegisterType<Third>().EnableClassInterceptors().InterceptedBy(typeof(ExceptionHandler));
            container = builder.Build();
            // 
            Demo3();


            Console.ReadLine();
        }

        private static void Demo1()
        {
            var first = container.Resolve<First>();
            Console.WriteLine("val:" + first.GetValue()); ;
        }

        private static void Demo2()
        {
            var second = container.Resolve<Second>();
            second.GetValue();
        }

        private static void Demo3()
        {
            var third = container.Resolve<Third>();
            third.Method1();
        }
    }
}